<template>
  <div class="container">
    <div class="left-side">
      <a-tree
        style="width: 280px; height: 680px"
        :default-selected-keys="departmentTreeDefault as any[]"
        :data="departmentTree"
        :show-line="true"
        :size="'mini'"
        block-node
        :virtual-list-props="departmentTreeConfig.virtualListProps"
        :field-names="departmentTreeConfig.fieldNames"
        @select="selectDept"
      >
        <!--      <template #icon>-->
        <!--        <IconStar />-->
        <!--      </template>-->
        <template #extra="nodeData">
          <IconPlus
            style="
              position: absolute;
              right: 2px;
              font-size: 12px;
              top: 10px;
              color: #3370ff;
            "
            @click="() => onIconClick(nodeData)"
          />
        </template>
      </a-tree>
    </div>
    <div class="right-side">
      <a-descriptions
        :data="departmentTreeSelectedDescription"
        :title="departmentTreeSelected.name"
        bordered
      />
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { fetchDepartmentAllTree } from '@/api/idm/department';
  import { DepartmentView } from '@/types/idm/department';

  function onIconClick(nodeData: any) {
    const children = nodeData.children || [];
    children.push({
      title: 'new tree node',
      key: `${nodeData.key}-${children.length + 1}`,
    });
    nodeData.children = children;

    departmentTree.value = [...departmentTree.value];
  }

  const departmentTreeConfig = ref({
    fieldNames: {
      title: 'name',
      key: 'id',
    },
    virtualListProps: {
      height: 600,
      threshold: 25,
    },
  });

  const departmentTreeSelected = ref({} as DepartmentView);
  const departmentTreeSelectedDescription = ref([] as any);
  const selectDept = (id: number, data: DepartmentView) => {
    departmentTreeSelected.value = data.node;
    departmentTreeSelectedDescription.value = [
      {
        label: '编码',
        value: data.node.code,
      },
      {
        label: '名称',
        value: data.node.name,
      },
      {
        label: '创建时间',
        value: data.node.whenCreated,
      },
    ];
  };

  const departmentTree = ref([] as DepartmentView[]);
  const departmentTreeDefault = [] as any;

  const treeDept = async (qry: any) => {
    const deptTreeRes = await fetchDepartmentAllTree(qry);
    departmentTree.value = deptTreeRes.data;
    const firstDept = deptTreeRes.data.at(0) as DepartmentView;
    departmentTreeDefault.push(firstDept.id);
    departmentTreeSelected.value = firstDept;
  };

  // 进入页面之后执行的第一个函数
  const onPageLoad = async () => {
    await treeDept({});
  };
  onPageLoad();
</script>

<script lang="ts">
  export default {
    name: 'PlatIdmOrgDept',
  };
</script>

<style lang="less" scoped>
  .container {
    padding: 4px 8px 4px 4px;
    background-color: var(--color-bg-2);
    width: 100%;
    height: 100%;
    display: flex;
  }

  .left-side {
    width: 280px;
  }

  .right-side {
    //width: 280px;
    margin-left: 16px;
  }

  .panel {
    background-color: var(--color-bg-2);
    border-radius: 4px;
    overflow: auto;
  }
  :deep(.panel-border) {
    margin-bottom: 0;
    border-bottom: 1px solid rgb(var(--gray-2));
  }
  .moduler-wrap {
    border-radius: 4px;
    background-color: var(--color-bg-2);
    :deep(.text) {
      font-size: 12px;
      text-align: center;
      color: rgb(var(--gray-8));
    }

    :deep(.wrapper) {
      margin-bottom: 8px;
      text-align: center;
      cursor: pointer;

      &:last-child {
        .text {
          margin-bottom: 0;
        }
      }
      &:hover {
        .icon {
          color: rgb(var(--arcoblue-6));
          background-color: #e8f3ff;
        }
        .text {
          color: rgb(var(--arcoblue-6));
        }
      }
    }

    :deep(.icon) {
      display: inline-block;
      width: 32px;
      height: 32px;
      margin-bottom: 4px;
      color: rgb(var(--dark-gray-1));
      line-height: 32px;
      font-size: 16px;
      text-align: center;
      background-color: rgb(var(--gray-1));
      border-radius: 4px;
    }
  }
</style>

<style lang="less" scoped>
  // responsive
  .mobile {
    .container {
      display: block;
    }
    .right-side {
      // display: none;
      width: 100%;
      margin-left: 0;
      margin-top: 16px;
    }
  }
</style>
